package com.develope.tree.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.develope.tree.login.User;

public class dbUser extends SQLiteOpenHelper {
	public static final String DATABASE_NAME = "MyDB.db";
	public static final String DATA_TABLE_NAME = "user";
	public static final int DATABASE_VERSION = 1;
	
	public static final String DATA_COLUMN_ID = "id";
	public static final String DATA_COLUMN_USER = "User";
	public static final String DATA_COLUMN_PASSWORD = "Pass";
	
	
	public dbUser(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table user"
				+ "(id integer primary key, User text, Pass text)");
		
		db.execSQL("create table data"
				+ "(id integer primary key, user text, lo text, cay text, ngay text, ngayNhap text, ttrang text, kthuoc text, "
				+ "cdinh text, bche text, chcao text, chvi text, la text, tcanh text, lco text, sbenh text, "
				+ "doam text, timebon text, luongbon text, timephun text, luongphun text, slcay text, "
				+ "slluong text, dgia text, vdkhac text, vtri text, sync text)");
		
		db.execSQL("create table userData"
				+ "(id integer primary key, User text, parcelId int, parcelName text)");
		
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS user");
		db.execSQL("DROP TABLE IF EXISTS data");
		db.execSQL("DROP TABLE IF EXISTS userData");
		this.onCreate(db);
	}
	
	public boolean insertUser(User user) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		
		contentValues.put("User", user.getUser());
		contentValues.put("Pass", user.getPass());
		db.insert("user", null, contentValues);
		return true;
	}
	
	public int numberOfRows() {
		SQLiteDatabase db = this.getReadableDatabase();
		int numRows = (int) DatabaseUtils.queryNumEntries(db, DATA_TABLE_NAME);
		return numRows;
	}
	
	public boolean updateUser(User user) {
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues contentValues = new ContentValues();
		
		contentValues.put("User", user.getUser());
		contentValues.put("Pass", user.getPass());
		
		db.update("user", contentValues, "User= ? ",
				new String[] { user.getUser()});
		return true;
	}
	
	public Integer deleteUser(Integer id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete("user", "id = ?",
				new String[] { Integer.toString(id) });
	}
	
	public boolean checkLogin(User user){
		SQLiteDatabase db = this.getReadableDatabase();
		String us= user.getUser();
		String pa= user.getPass();
		
		Cursor c = db.rawQuery("select * from "+DATA_TABLE_NAME+" where "+DATA_COLUMN_USER+" = ? and "+DATA_COLUMN_PASSWORD+" = ?", new String[] { us,pa });
		
		if(c.getCount()==1){
			c.close();
			return true;
		} else {
			c.close();
			return false;
		}
	}
	
	
	
}
